#!/usr/bin/python -S
"""
parse_test.py: Tests for parse.py
"""

__author__ = 'Andy Chu'


import os
import re
import sys

if __name__ == '__main__':
  # for testy
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..',
    '..', 'svn', 'pan', 'trunk'))

from pan.core import cmdapp
from pan.core import params
from pan.core import json
from pan.core import util
from pan.test import testy

import parse  # module under test
import _jsonpattern as jsonpattern


class ParseTest(testy.Test):
  def setUp(self):
    pass

  def tearDown(self):
    pass

  def verifyBlockParse(self, s, expected):
    self.verify.RegexGroups(parse.BLOCK_RE, s, expected)

  def testBlockRe(self):
    self.verifyBlockParse(
        'value foo', ('', 'value', None, 'foo'))
    self.verifyBlockParse(
        'value foo', ('', 'value', None, 'foo'))
    self.verifyBlockParse(
        'value foo ', ('', 'value', None, 'foo'))
    self.verifyBlockParse(
        '$value foo', ('$', 'value', None, 'foo'))
    self.verifyBlockParse(
        '$repeated * foo', ('$', 'repeated', '*', 'foo'))
    self.verifyBlockParse(
        '$repeated 3,6 foo', ('$', 'repeated', '3,6', 'foo'))
    self.verifyBlockParse(
        'repeated 3,6 ', ('', 'repeated', '3,6', None))
    self.verifyBlockParse(
        '$repeated 3,6 ', ('$', 'repeated', '3,6', None))
    self.verifyBlockParse(
        'repeated +', ('', 'repeated', '+', None))
    self.verifyBlockParse(
        'repeated', ('', 'repeated', None, None))
    self.verifyBlockParse(
        '$repeated', ('$', 'repeated', None, None))
    self.verifyBlockParse(
        'choice', ('', 'choice', None, None))
    self.verifyBlockParse(
        '$value ', ('$', 'value', None, None))

    self.verifyBlockParse('choices', None)
    self.verifyBlockParse('blah', None)

  def verifyShorthandParse(self, s, expected):
    self.verify.RegexGroups(parse.SHORTHAND_RE, s, expected)

  def testShorthandRe(self):
    self.verifyShorthandParse('yo', ('yo',))
    self.verifyShorthandParse(' yo', ('yo',))
    # An anonymous variable
    self.verifyShorthandParse(' ', (None,))

  def verifyGroupCapture(self, s, expected):
    self.verify.RegexGroups(parse.GROUP_CAPTURE_RE, s, expected)

  def testGroupCaptureRe(self):
    self.verifyGroupCapture('groups[] foo', (None, 'foo'))
    self.verifyGroupCapture('group[1] foo', ('1', 'foo'))
    self.verifyGroupCapture('group[1] ', ('1', None))
    self.verifyGroupCapture('group[1]', ('1', None))
    self.verifyGroupCapture('group[]', (None, None))

  def testParseFilters(self):
    self.verify.Equal(parse.ParseFilters(''), [])
    self.verify.Equal(parse.ParseFilters('|foo'), ['foo'])
    self.verify.Equal(parse.ParseFilters('|foo|bar'), ['foo', 'bar'])
    self.verify.Equal(parse.ParseFilters(' | foo! | ~bar'), ['foo!', '~bar'])

  def testAsTree(self):
    print jsonpattern.Pattern('P| foo').AsTree()
    print '-'* 80
    print jsonpattern.Pattern('P| { .* foo}').AsTree()
    print '-'* 80
    print jsonpattern.Pattern("""
         | repeated poo
        P|   { .* foo}
        P|   yo
         | end
        """).AsTree()
    print '-'* 80
    print jsonpattern.Pattern("""
         | repeated yo
         |   value bar
        P|     { .* foo}
        P|     yo
         |   end 
         | end
        """).AsTree()


if __name__ == '__main__':
  testy.RunThisModule()
